import React, {useEffect, useState} from 'react'
import {Button, Space, message, Cascader, Select, TreeSelect} from 'antd'
import type {ColumnsType} from 'antd/es/table'
import {history, connect} from 'umi'
import styles from './index.less'
import SearchForm from '@/components/SearchForm'
import Permissions from '@/components/Permissions'
import {getGraderecordList} from '@/service/supplier/graderecord'
import {getDict} from '@/service/common/dict'
import {getSupplierType, translateDataToTree} from '@/utils/common'
import CustomTable from '@/components/Table'
import {getCompany} from '@/service/common'

interface DataType {
	id: string | string[] | null
	supplierCode: string
	supplierName: string
	orgId: string
	serviceType: string
	contacts: string
	status: string
	date: string
}

const formItemLayout = {
	labelCol: {flex: '90px'},
	wrapperCol: {flex: '1'},
}

const fieldNames = {
	label: 'name',
	value: 'id',
	id: 'id',
	pId: 'parentId',
}

const grade = (props: {user: {orgId: string | number}}) => {
	const [dataSourse, setDataSourse] = useState([])
	const [searchData, setSearchData] = useState({})
	const [loading, setLoading] = useState<boolean>(false)
	// 字典数据
	const [dictData, setDictData] = useState<any>({})
	const [supplierTypeData, setSupplierTypeData] =
		useState<supplierAPI.supplierType>([])
	const [companyData, setCompanyData] = useState([])
	const [total, setTotal] = useState()
	const [params, setParams] = useState({
		current: 1,
		size: 10,
	})

	// 获取公司列表
	const onCompany = () => {
		getCompany({merchantId: props.user.orgId}).then((res: any) => {
			const list = translateDataToTree(res.data)
			setCompanyData(list as any)
		})
	}

	useEffect(() => {
		onCompany()
	}, [props.user.orgId])

	// 获取供应商评级列表
	const getData = () => {
		setLoading(true)
		getGraderecordList({...params, ...searchData})
			.then((res: any) => {
				if (res?.success) {
					setDataSourse(res?.data?.records)
					setTotal(res?.data?.total)
					setLoading(false)
				}
			})
			.catch((err: any) => {
				setDataSourse([])
				message.error(err)
				setLoading(false)
			})
	}
	useEffect(() => {
		getData()
	}, [params])
	useEffect(() => {
		getDict(
			{
				dictKeys: [
					'ZC_GYS',
					'JC_GYS',
					'CZ_GYS',
					'SP_GYS',
					'DYY_GYS',
					'CS_GYS',
					'Rating_Cycle',
					'Supplier_Rating',
				],
			},
			(res: any) => {
				setDictData(res)
				// 组装供应商类型
				const newData = getSupplierType([
					res.ZC_GYS,
					res.JC_GYS,
					res.CZ_GYS,
					res.SP_GYS,
					res.DYY_GYS,
					res.CS_GYS,
				])
				setSupplierTypeData(newData)
			},
		)
	}, [])

	/** 点击详情及编辑事件 */
	const onDetails = (type: string, record: DataType) => {
		history.push({
			pathname: `/supplier/grades/${type}`,
			query: {
				id: record?.id,
			},
		})
	}

	const columns: ColumnsType<DataType> = [
		{
			title: '供应商编号',
			dataIndex: 'supplierCode',
			align: 'center',
			width: 150,
		},
		{
			title: '供应商名称',
			dataIndex: 'supplierName',
			align: 'center',
			width: 150,
		},
		{
			title: '供应商所属公司',
			dataIndex: 'orgId',
			align: 'center',
			width: 200,
			render: (text) => (
				<>
					{text ? (
						<TreeSelect
							value={text?.split(',') || []}
							disabled
							dropdownStyle={{maxHeight: 400, overflow: 'auto'}}
							treeDefaultExpandAll
							multiple
							showCheckedStrategy="SHOW_ALL"
							fieldNames={fieldNames}
							treeDataSimpleMode
							treeData={companyData}
						/>
					) : (
						'-'
					)}
				</>
			),
		},
		{
			title: '供应商类型',
			dataIndex: 'supplierType',
			align: 'center',
			width: 150,
			render: (text) => (
				<Cascader
					disabled
					value={text.split(',')}
					options={supplierTypeData}
				/>
			),
		},
		{
			title: '供应商联系人/电话',
			dataIndex: 'contactPhone',
			align: 'center',
			width: 180,
		},
		{
			title: '最近更新时间',
			dataIndex: 'updateTime',
			align: 'center',
			width: 200,
		},
		{
			title: '评级周期',
			dataIndex: 'ratingCycle',
			align: 'center',
			width: 100,
			render: (text) => (
				<Select
					value={text}
					disabled
					fieldNames={{value: 'dictKey', label: 'dictValue'}}
					options={dictData?.Rating_Cycle}
				/>
			),
		},
		{
			title: '供应商综合评分',
			dataIndex: 'score',
			align: 'center',
			width: 150,
		},
		{
			title: '供应商评级',
			dataIndex: 'scoreGrade',
			align: 'center',
			width: 120,
			render: (text) => (
				<Select
					value={text}
					disabled
					fieldNames={{value: 'dictKey', label: 'dictValue'}}
					options={dictData?.Supplier_Rating}
				/>
			),
		},
		{
			title: '操作',
			key: 'action',
			align: 'center',
			width: 120,
			fixed: 'right',
			render: (text: any, record) => (
				<Space size={12}>
					<Permissions permissionID="amc_supplier_grade_view">
						<Button
							type="link"
							onClick={() => {
								onDetails('details', record)
							}}>
							详情
						</Button>
					</Permissions>
					<Permissions permissionID="amc_supplier_grade_edit">
						<Button
							type="link"
							onClick={() => {
								onDetails('edit', record)
							}}>
							修改
						</Button>
					</Permissions>
				</Space>
			),
		},
	]
	const formItems: any[] = [
		{
			type: 'group',
			key: 'group1',
			groupItems: [
				{
					type: 'input',
					label: '供应商编号',
					name: 'supplierCode',
					key: 'supplierCode',
				},
				{
					type: 'input',
					label: '供应商名称',
					name: 'supplierName',
					key: 'supplierName',
				},
				{
					type: 'cascader',
					key: 'supplierType',
					name: 'supplierType',
					label: '供应商类型',
					items: supplierTypeData,
				},
			],
		},
	]
	const onFinish = (values: any) => {
		const seachData = {
			...values,
			supplierType: values?.supplierType?.toString(),
		}
		setSearchData(seachData)
		setParams({
			current: 1,
			size: 10,
		})
	}

	const onReset = () => {
		setSearchData({})
		setParams({
			current: 1,
			size: 10,
		})
	}

	return (
		<div className={styles.admittance}>
			<div className={styles.content}>
				<div className={styles.search}>
					<SearchForm
						onFinish={onFinish}
						onReset={onReset}
						formItems={formItems}
						formLayout={formItemLayout}
					/>
				</div>
				<CustomTable
					params={params}
					columns={columns}
					total={total}
					pagination
					callback={(pagination: any) => {
						setParams(pagination)
					}}
					loading={loading}
					dataSource={dataSourse}
				/>
			</div>
		</div>
	)
}
export default connect(({user}: any) => ({user}))(grade)
